Skip to content

Fix CollectionMembershipOperator for collections with mixed element types - #1370

Open
AlpriElse wants to merge 2 commits into
HubSpot:masterfrom
AlpriElse:fix/in-operator-mixed-types
Open

AlpriElse wants to merge 2 commits into
HubSpot:masterfrom
AlpriElse:fix/in-operator-mixed-types

Conversation

@AlpriElse

@AlpriElse AlpriElse commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Partially addresses #1240.

Context

CollectionMembershipOperator is the operator used in this expression 1 in [1, 2, 3]

Instead of calling the operands o1 and o2, this pull requests will use <needle> in <collection>

Problem

CollectionMembershipOperator converted the needle to the type of the first non-null element and then called contains once.

Broken Examples

{# zero = 0 from the context (Integer); literals are Long #}
{{ 1 in [zero, 1] }}     {# evaluates to false; expected true #}

{# 2 converted to '2' #}
{{ 2 in ['1', 2] }}      {# evaluates to false; expected true #}

{# 'x' can't be converted to Long #} 
{{ 'x' in [1, 'x'] }}    {# evaluates to false; expected true #}

Confirmed that these semantics don't match Jinja2:
https://gist.github.com/AlpriElse/c70e301761c3b1b525af333fc407fb76

What this PR Does

The needle is now converted to each non-null element's type in turn, still checking with contains (lists) or containsKey (maps); the first match returns true. Single-typed collections give the same results as before. The null needle path is unchanged on purpose (see below).

Tests

  • New in CollectionMembershipOperatorTest: itChecksIfMixedTypeArrayContainsValue and itChecksIfMixedTypeDictionaryContainsKey. Both fail on master and pass with this change.
  • CollectionMembershipOperatorTest, CollectionNonMembershipOperatorTest and IsContainingExpTestTest all pass (17 tests).

Results for the examples in #1240

Fixed (these now match Jinja2 3.1.6):

Expression Before After
2 in ['1', 2] false true
1 in [false, 1] false true
1 in [null, 'abc', 1] false true
'' in [1, '', 2] false true
[false, 1] is containing 1 false true

#1240 examples kept out of scope (for backwards compatibility)

null type coercion semantics

Expression Before fix Fixed branch Jinja2 Why
null in [1, null, 2] false false true null is converted to the first element's type: it becomes 0, and the list has no 0
null in [0, 1, 2] true true false Same conversion: null becomes 0, which matches the 0

Existing tests that depend on the null behavior:

string/number type coercion semantics

Expression Before fix Fixed branch Jinja2 Why
1 in ['1', 2] true true false 1 is converted to the first element's type: it becomes '1', which matches
'' in [0, 1, 2] true true false '' is converted to a number, which gives 0 and matches

Existing tests that depend on string/number conversion in in and its aliases:

@AlpriElse AlpriElse changed the title Fix CollectionMembershipOperator for dicts and lists with mixed element types Fix CollectionMembershipOperator for collections with mixed element types Nov 24, 2026
@AlpriElse
AlpriElse marked this pull request as ready for review November 24, 2026 07:49
@AlpriElse

Copy link
Copy Markdown
Contributor Author

whats up @jasmith-hs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant